<?php

namespace app\controller;

use app\BaseController;


class Department extends Base
{

    public function initialize()
    {
        parent::initialize();
        Token::apisecurity();
    }

    /**
     * 部门列表
     * api post /department/index
     * @param int id 部门ID
     * @return json
     */
    public function index()
    {
        if (_isget()) {
            if (isset(_get()['id']) && _get()['id']) {
                $where = [];
                $where[] = ['delete_time', '=', 0];
                $where[] = ['id', '=',  _get()['id']];
                $result = db('department', 'mysql')->where($where)->find();
                if (!$result) {
                    _Error('部门不存在');
                }

                $result['create_time'] = totime($result['create_time']);
                $result['update_time'] = totime($result['update_time']);
                _Success('部门详情', 200, $result);
            } else {
                $where = [];
                $where[] = ['delete_time', '=', 0];

                $list = db('department', 'mysql')->where($where)->cache('getDepartmentSelect')->select()->each(function ($item) {
                    $item['create_time'] = totime($item['create_time']);
                    $item['update_time'] = totime($item['update_time']);
                    return $item;
                });
                _Success('部门列表', 200, $list);
            }
        }
        _Result('响应失败', 500);
    }

    /**
     * 添加部门
     * api post /department/department_add
     * @param string department_name 部门名称
     * @param string department_desc 部门描述
     * @return json
     */
    public function department_add()
    {
        if (_ispost()) {
            if (!isset(_post()['department_name']) || !_post()['department_name']) {
                _Error('部门名称未填写');
            }
            $data = _post();
            $data['create_time'] = time();
            $data['update_time'] = time();
            $result = db('department', 'mysql')->cache('getDepartmentSelect')->insert($data);
            if (!$result) {
                _Error('添加失败');
            }
            return _Success('添加成功');
        }
        _Result('响应失败', 500);
    }

    /**
     * 更新部门
     * api post /department/department_edit
     * @param int id 部门ID
     * @param string department_name 部门名称
     * @param string department_desc 部门描述
     * @return json
     */
    public function department_edit()
    {
        if (_ispost()) {
            if (!isset(_get()['id']) || !_get()['id']) {
                _Error('部门主键未提交');
            }
            $data = _post();
            $data['update_time'] = time();
            $result = db('department', 'mysql')->cache('getDepartmentSelect')->where('id', $data['id'])->update($data);
            if (!$result) {
                _Error('更新失败');
            }
            return _Success('更新成功');
        }
        _Result('响应失败', 500);
    }

    /**
     * 删除部门
     * @param int id 部门ID
     */
    public function department_del()
    {
        if (_ispost()) {
            if (!isset(_post()['id']) || !_post()['id']) {
                _Error('部门主键未提交');
            }
            if (db('users', 'mysql')->where('department_id', _post()['id'])->find()) {
                _Error('部门下有员工信息');
            }
            $result = db('department', 'mysql')->where('id', _post()['id'])->cache('getDepartmentSelect')->delete();
            if (!$result) {
                _Error('删除失败');
            }
            return _Success('删除成功');
        }
        _Result('响应失败', 500);
    }
}
